
import numpy as np
import scipy
from matplotlib import pyplot as plt
from matplotlib import animation
from scipy.signal import find_peaks

data_red = [135264, 139383, 139386, 139378, 139379, 139402,
            139419, 139418, 139426, 139444, 139438, 139453, 139452,
            139460, 139459, 139466, 139439, 139349, 139330, 139350,
            139385, 139386, 139399, 139421, 139413, 139414, 139425,
            139437, 139445, 139460, 139460, 139449, 139468, 139460,
            139458, 139462, 139464, 139456, 139453, 139465, 139371,
            139310, 139326, 139347, 139352, 139362, 139376, 139378,
            139373, 139358, 139365, 139384, 139391, 139400, 139411,
            139410, 139414, 139432, 139433, 139431, 139441, 139452,
            139436, 139358, 139317, 139348, 139363, 139374, 139395,
            139397, 139382, 139382, 139392, 139413, 139425, 139434,
            139431, 139444, 139452, 139457, 139465, 139472, 139488,
            139491, 139471, 139389, 139365, 139383, 139399, 139417,
            139418, 139443, 139428, 139431, 139435, 139446, 139464,
            139476, 139480, 139480, 139488, 139491, 139491, 139503,
            139502, 139515, 139517, 139454, 139382, 139388, 139421,
            139433, 139438, 139446, 139450, 139450, 139442, 139453,
            139480, 139504, 139512, 139532, 139545, 139544, 139535,
            139550, 139541, 139547, 139557, 139571, 139566, 139568,
            139482, 139442, 139462, 139499, 139510, 139510, 139514,
            139514, 139515, 139518, 139533, 139542, 139559, 139560,
            139569, 139571, 139569, 139562, 139568, 139577, 139568,
            139583, 139578, 139586, 139560, 139479, 139458, 139484,
            139501, 139505, 139513, 139515, 139513, 139521, 139529,
            139547, 139562, 139571, 139572, 139566, 139571, 139563,
            139572, 139570, 139573, 139575, 139576, 139564, 139497,
            139454, 139474, 139500, 139512, 139511, 139528, 139534,
            139520, 139521, 139524, 139545, 139551, 139559, 139574,
            139573, 139584, 139576, 139567, 139567, 139567, 139570, 139567, 139578, 139522, 139438, 139448, 139479, 139496, 139504, 139518, 139522, 139515, 139508, 139517, 139531, 139536, 139530, 139532, 139538, 139548, 139561, 139576, 139590, 139592, 139592, 139603, 139602, 139604, 139600, 139520, 139460, 139471, 139506, 139519, 139538, 139539, 139531, 139529, 139530, 139525, 139551, 139569, 139574, 139582, 139572, 139578, 139577, 139594, 139584, 139577, 139584, 139591, 139602, 139594, 139549, 139473, 139468, 139475, 139491, 139604, 139487, 139498, 139511, 139499, 139500, 139527, 139531, 139533, 139539, 139537, 139532, 139542, 139532, 139534, 139541, 139553, 139571, 139560, 139566, 139489, 139459, 139469, 139492, 139507, 139505, 139514, 139516, 139493, 139498, 139509, 139522, 139530, 139540, 139537, 139538, 139545, 139555, 139559, 139562, 139568, 139573, 139547, 139479, 139463, 139477, 139498, 139508, 139519, 139523, 139519, 139504, 139515, 139531, 139532, 139536, 139541, 139554, 139560, 139548, 139549, 139543, 139553, 139559, 139561, 139572, 139516, 139452, 139456, 139480, 139488, 139486, 139488, 139499, 139490, 139491, 139495, 139510, 139515, 139512, 139519, 139527, 139534, 139545, 139551, 139552, 139568, 139571, 139541, 139458, 139452, 139473, 139494, 139503, 139518, 139520, 139513, 139511, 139522, 139541, 139539, 139559, 139572, 139586, 139582, 139585, 139588, 139598, 139613, 139618, 139600, 139526, 139505, 139535, 139549, 139546, 139552, 139560, 139560, 139556, 139561, 139565, 139569, 139573, 139605, 139603, 139617, 139635, 139629, 139622, 139639, 139650, 139645, 139646, 139644, 139646, 139573, 139502, 139502, 139532, 139553, 139547, 139545, 139537, 139539, 139540, 139559, 139560, 139582, 139578, 139590, 139588, 139609, 139622, 139637, 139646, 139655, 139639, 139647, 139648, 139658, 139666, 139637, 139570, 139557, 139563, 139565, 139569, 139588, 139604, 139590, 139590, 139587, 139605, 139601, 139602, 139612, 139600, 139616, 139618, 139619, 139620, 139622, 139628, 139639, 139645, 139584, 139516, 139524, 139538, 139548, 139536, 139549, 139550, 139544, 139546, 139557, 139574, 139580, 139590, 139594, 139590, 139590, 139596, 139597, 139609, 139612, 139620, 139613, 139608, 139619, 139596, 139501, 139475, 139520, 139531, 139523, 139539, 139545, 139551, 139561, 139570, 139574, 139580, 139585, 139596, 139600, 139596, 139606, 139612, 139616, 139624, 139613, 139625, 139624, 139625, 139634, 139627, 139550, 139514, 139526, 139548, 139558, 139562, 139561, 139551, 139545, 139538, 139549, 139557, 139555, 139561, 139562, 139556, 139548, 139545, 139557, 139589, 139600, 139596, 139599, 139610, 139634, 139629, 139553, 139528, 139554, 139597, 139610, 139640, 139661, 139664, 139669, 139684, 139681, 139690, 139706, 139707, 139712, 139717, 139728, 139740, 139734, 139741, 139747, 139696, 139619, 139621, 139671, 139682, 139684, 139689, 139686, 139673, 139685, 139700, 139717, 139712, 139718, 139734, 139731, 139728, 139731, 139712, 139706, 139724, 139718, 139730, 139705, 139614, 139591, 139613, 139648, 139657, 139653, 139662, 139685, 139698, 139710, 139714, 139732, 139718, 139726, 139721, 139719, 139739, 139733, 139728, 139732, 139731, 139726, 139740, 139738, 139741, 139753, 139740, 139656, 139636, 139664, 139679, 139676, 139668, 139678, 139683, 139681, 139677, 139696, 139709, 139712, 139713, 139711, 139706, 139708, 139713, 139721, 139719, 139731, 139748, 139733, 139738, 139745, 139734, 139645, 139628, 139663, 139680, 139690, 139708, 139708, 139699, 139701, 139709, 139707, 139710, 139722, 139712, 139728, 139725, 139741, 139734, 139751, 139767, 139758, 139765, 139773, 139739, 139655, 139638, 139654, 139670, 139684, 139691, 139694, 139699, 139720, 139708, 139716, 139736, 139733, 139736, 139746, 139736, 139747, 139754, 139755, 139759, 139768, 139775, 139781, 139792, 139770, 139705, 139683, 139712, 139748, 139750, 139762, 139780, 139784, 139792, 139781, 139796, 139796, 139812, 139801, 139807, 139812, 139846, 139851, 139857, 139865, 139868, 139873, 139866, 139875, 139892, 139867, 139786, 139765, 139793, 139798, 139805, 139813, 139821, 139833, 139855, 139862, 139873, 139893, 139892, 139893, 139896, 139880, 139866, 139873, 139867, 139878, 139886, 139886, 139884, 139833, 139804, 139834, 139866, 139885, 139900, 139903, 139889, 139875, 139880, 139947, 140043, 140093, 140111, 140127, 140148, 140139, 140127, 140134, 140153, 140144, 140156, 140156, 140091, 140006, 139983, 139987, 139970, 139953, 139939, 139911, 139896, 139870, 139862, 139866, 139879, 139901, 139916, 139928, 139922, 139932, 139929, 139922, 139926, 139936, 139949, 139966, 139904, 139837, 139846, 139868, 139875, 139886, 139912, 139914, 140091, 139916, 139932, 139939, 139947, 139945, 139969, 139986, 139983, 139966, 139976, 139968, 139980, 139988, 139995, 139999, 139969, 139890, 139891, 139914, 139934, 139939, 139942, 139957, 139972, 139976, 139971, 139990, 140006, 140014, 140019, 140006, 140026, 140027, 140046, 140051, 140007, 139924, 139902, 139923, 139950, 139965, 139987, 139995, 139996, 139990, 139991, 140002, 139998, 140009, 140022, 140012, 140025, 140048, 140062, 140071, 140064, 140044, 140024, 139932, 139913, 139933, 139959, 139967, 139981, 139987, 139993, 139991, 139999, 140007, 140026, 140044, 140050, 140067, 140068, 140074, 140082, 140094, 140098, 140122, 140125, 140112, 140113, 140083, 139977, 139950, 139980, 139989, 139999, 139998, 139995, 140014, 140018, 140017, 140016, 140033, 140030, 140022, 140024, 140024, 140030, 140019, 140024, 140036, 140024, 140042, 140050, 140057, 140062, 140067, 139983, 139931, 139940, 139975, 139984, 139987, 140002, 139998, 139990, 139986, 139998, 140018, 140037, 140039, 140036, 140045, 140031, 140040, 140035, 140053, 140054, 140051, 140031, 139934, 139903, 139934, 139953, 139953, 139963, 139966, 139981, 139977, 139971, 139994, 139992, 139994, 140004, 140003, 140002, 139994, 139984, 139988, 140002, 140022, 140023, 140014, 140026, 139989, 139904, 139885, 139898, 139905, 139906, 139897, 139898, 139885, 139885, 139890, 139895, 139911, 139916, 139922, 139940, 139940, 139938, 139946, 139933, 139936, 139936, 139934, 139924, 139923, 139927, 139937, 139895, 139809, 139786, 139823, 139845, 139846, 139843, 139825, 139794, 139756, 139738, 139743, 139754, 139773, 139768, 139776, 139775, 139775, 139776, 139787, 139763, 139760, 139752, 139723, 139693, 139653, 139630, 139606, 139508, 139479, 139518, 139543, 139544, 139554, 139555, 139544, 139558, 139575, 139605, 139626]

data_ir = [119485, 119481, 119488, 119493, 119489, 119494, 119502, 119494, 119497, 119505, 119506, 119512, 119521, 119517, 119513, 119510, 119501, 119474, 119472, 119477, 119493, 119496, 119499, 119499, 119507, 119508, 119524, 119523, 119529, 119529, 119527, 119519, 119524, 119527, 119523, 119517, 119515, 119501, 119497, 119505, 119472, 119444, 119449, 119458, 119459, 119463, 119477, 119487, 119478, 119472, 119478, 119468, 119469, 119477, 119492, 119485, 119482, 119491, 119498, 119492, 119489, 119497, 119482, 119454, 119441, 119455, 119461, 119466, 119469, 119469, 119461, 119470, 119465, 119468, 119482, 119474, 119474, 119486, 119493, 119494, 119485, 119493, 119501, 119500, 119495, 119477, 119460, 119475, 119468, 119480, 119479, 119486, 119482, 119478, 119479, 119483, 119481, 119492, 119499, 119497, 119501, 119493, 119505, 119512, 119503, 119509, 119514, 119486, 119468, 119474, 119483, 119487, 119482, 119492, 119495, 119489, 119482, 119492, 119502, 119516, 119525, 119533, 119547, 119550, 119551, 119551, 119545, 119549, 119556, 119565, 119558, 119557, 119528, 119507, 119528, 119535, 119538, 119544, 119535, 119534, 119537, 119543, 119542, 119543, 119558, 119555, 119565, 119565, 119563, 119557, 119559, 119558, 119549, 119553, 119549, 119542, 119542, 119521, 119520, 119530, 119522, 119518, 119520, 119528, 119526, 119534, 119535, 119547, 119551, 119548, 119555, 119552, 119545, 119539, 119536, 119549, 119551, 119547, 119541, 119543, 119521, 119503, 119508, 119516, 119515, 119524, 119532, 119536, 119530, 119532, 119529, 119535, 119534, 119546, 119564, 119547, 119554, 119559, 119554, 119540, 119544, 119537, 119538, 119545, 119519, 119496, 119500, 119510, 119515, 119516, 119526, 119527, 119525, 119520, 119519, 119523, 119526, 119530, 119521, 119525, 119539, 119539, 119557, 119565, 119562, 119555, 119560, 119562, 119566, 119556, 119532, 119506, 119518, 119519, 119525, 119533, 119534, 119531, 119530, 119531, 119523, 119531, 119538, 119538, 119542, 119538, 119529, 119536, 119536, 119540, 119536, 119531, 119540, 119543, 119532, 119518, 119499, 119488, 119488, 119483, 119566, 119475, 119483, 119497, 119491, 119501, 119503, 119503, 119505, 119498, 119499, 119504, 119499, 119492, 119494, 119497, 119505, 119518, 119525, 119513, 119496, 119492, 119486, 119495, 119501, 119491, 119492, 119488, 119490, 119489, 119496, 119497, 119505, 119498, 119500, 119504, 119510, 119508, 119507, 119516, 119507, 119513, 119499, 119492, 119481, 119488, 119504, 119506, 119511, 119508, 119506, 119499, 119498, 119520, 119506, 119514, 119516, 119525, 119512, 119509, 119512, 119507, 119525, 119505, 119512, 119518, 119504, 119478, 119486, 119489, 119480, 119485, 119482, 119491, 119486, 119491, 119490, 119492, 119484, 119494, 119499, 119503, 119503, 119506, 119512, 119518, 119525, 119525, 119510, 119498, 119484, 119489, 119488, 119496, 119503, 119509, 119500, 119493, 119511, 119511, 119504, 119513, 119537, 119533, 119544, 119534, 119537, 119551, 119547, 119555, 119540, 119527, 119519, 119548, 119547, 119542, 119544, 119548, 119544, 119544, 119542, 119548, 119530, 119531, 119550, 119554, 119569, 119580, 119575, 119564, 119582, 119575, 119571, 119561, 119572, 119568, 119539, 119516, 119513, 119528, 119540, 119539, 119537, 119524, 119529, 119528, 119530, 119537, 119541, 119550, 119542, 119550, 119554, 119570, 119579, 119588, 119579, 119578, 119581, 119570, 119578, 119580, 119585, 119580, 119558, 119559, 119551, 119542, 119559, 119565, 119566, 119561, 119553, 119555, 119552, 119559, 119558, 119557, 119562, 119557, 119556, 119557, 119550, 119551, 119555, 119567, 119546, 119520, 119518, 119526, 119520, 119522, 119525, 119519, 119516, 119513, 119524, 119528, 119531, 119531, 119531, 119534, 119529, 119527, 119531, 119533, 119537, 119532, 119525, 119516, 119526, 119518, 119493, 119489, 119497, 119499, 119497, 119493, 119499, 119508, 119508, 119517, 119522, 119520, 119519, 119520, 119529, 119520, 119522, 119542, 119534, 119538, 119539, 119535, 119533, 119533, 119537, 119533, 119516, 119509, 119502, 119509, 119510, 119506, 119504, 119492, 119493, 119485, 119480, 119483, 119486, 119485, 119482, 119477, 119474, 119475, 119472, 119489, 119529, 119494, 119504, 119508, 119530, 119536, 119506, 119506, 119514, 119533, 119545, 119552, 119579, 119580, 119578, 119586, 119586, 119594, 119598, 119599, 119591, 119601, 119614, 119620, 119612, 119614, 119621, 119593, 119571, 119583, 119601, 119604, 119600, 119609, 119600, 119593, 119596, 119608, 119611, 119608, 119619, 119621, 119622, 119620, 119607, 119593, 119590, 119598, 119588, 119590, 119585, 119559, 119553, 119547, 119567, 119584, 119580, 119583, 119597, 119609, 119610, 119613, 119615, 119622, 119615, 119602, 119602, 119616, 119606, 119602, 119605, 119606, 119603, 119610, 119611, 119595, 119618, 119608, 119592, 119586, 119595, 119594, 119590, 119581, 119595, 119596, 119591, 119578, 119600, 119612, 119605, 119601, 119598, 119602, 119596, 119597, 119609, 119610, 119617, 119623, 119615, 119614, 119613, 119620, 119580, 119581, 119602, 119600, 119603, 119611, 119612, 119601, 119605, 119609, 119606, 119607, 119610, 119601, 119602, 119615, 119613, 119617, 119628, 119637, 119632, 119633, 119633, 119622, 119581, 119584, 119584, 119588, 119603, 119595, 119602, 119599, 119620, 119607, 119607, 119611, 119607, 119614, 119617, 119605, 119606, 119617, 119617, 119620, 119627, 119628, 119628, 119635, 119639, 119618, 119607, 119616, 119633, 119648, 119648, 119655, 119663, 119661, 119654, 119654, 119657, 119666, 119652, 119657, 119666, 119695, 119700, 119699, 119699, 119704, 119692, 119703, 119706, 119716, 119709, 119685, 119672, 119681, 119674, 119674, 119677, 119684, 119699, 119711, 119712, 119715, 119726, 119718, 119716, 119721, 119701, 119691, 119696, 119702, 119708, 119701, 119706, 119707, 119681, 119692, 119713, 119721, 119738, 119743, 119729, 119718, 119715, 119718, 119774, 119836, 119869, 119890, 119897, 119903, 119900, 119886, 119894, 119903, 119894, 119895, 119896, 119864, 119823, 119803, 119795, 119774, 119759, 119754, 119726, 119716, 119698, 119695, 119690, 119702, 119715, 119734, 119736, 119730, 119736, 119729, 119725, 119724, 119729, 119743, 119753, 119733, 119701, 119705, 119705, 119701, 119716, 119726, 119728, 119864, 119729, 119740, 119750, 119751, 119750, 119760, 119771, 119770, 119753, 119758, 119754, 119753, 119772, 119768, 119763, 119755, 119731, 119734, 119745, 119744, 119747, 119757, 119768, 119770, 119766, 119770, 119782, 119798, 119791, 119796, 119790, 119787, 119806, 119807, 119816, 119798, 119764, 119753, 119749, 119760, 119773, 119788, 119797, 119785, 119796, 119796, 119787, 119787, 119789, 119802, 119791, 119800, 119819, 119817, 119818, 119808, 119802, 119786, 119753, 119742, 119753, 119766, 119767, 119777, 119789, 119789, 119782, 119794, 119796, 119803, 119813, 119822, 119828, 119830, 119839, 119824, 119836, 119846, 119855, 119857, 119840, 119834, 119821, 119787, 119778, 119788, 119783, 119787, 119783, 119795, 119797, 119805, 119804, 119792, 119802, 119797, 119787, 119786, 119785, 119786, 119784, 119795, 119792, 119791, 119799, 119808, 119801, 119813, 119813, 119785, 119769, 119772, 119781, 119780, 119774, 119784, 119783, 119785, 119779, 119781, 119784, 119804, 119797, 119796, 119790, 119791, 119789, 119784, 119779, 119793, 119787, 119770, 119734, 119727, 119750, 119752, 119748, 119746, 119750, 119766, 119766, 119760, 119771, 119764, 119754, 119765, 119755, 119758, 119753, 119745, 119746, 119754, 119762, 119768, 119772, 119767, 119761, 119730, 119725, 119721, 119712, 119704, 119692, 119688, 119681, 119677, 119692, 119689, 119698, 119699, 119708, 119725, 119731, 119723, 119717, 119706, 119706, 119699, 119700, 119689, 119681, 119687, 119689, 119667, 119643, 119642, 119663, 119676, 119695, 119680, 119674, 119652, 119628, 119613, 119610, 119619, 119613, 119607, 119608, 119610, 119605, 119601, 119616, 119605, 119606, 119594, 119565, 119553, 119526, 119505, 119487, 119451, 119445, 119468, 119465, 119475, 119472, 119469, 119472, 119471, 119485, 119496, 119512]
data_spo2 = []

i = 0
while i < len(data_red):
        R = data_red[i]/(data_ir[i]+data_red[i])
        data = 110 - 25*R
        data_spo2.append(data)
        i = i+1
data_spo2 = data_spo2[3:len(data_spo2)]
x = np.linspace(0, 5000, len(data_spo2))
print(np.array(data_spo2))
data_h = np.fft.fft(data_red, n=None, axis=-1, norm=None)
[pks, locs] = find_peaks(data_h, height=None, threshold=None, distance=5,
                        prominence=None, width=None, wlen=None, rel_height=None,
                        plateau_size=None)
print(pks)
print("心率为:每分钟{}下".format(len(pks)/2))
Threshold = (max(data_red) - min(data_red)) * 0.4 + min(data_red)
# print('-----------Threshold---------------------')
# max(result)
# min(result)
# Threshold
# result
# # 100,'MinPeakDistance',0.8,'MinPeakHeight',
# locs =find_peaks(result, height=None, threshold=Threshold, distance=5,
#                prominence=None, width=None, wlen=None, rel_height=None,
#                plateau_size=None)
# #这里因为已经填写了100Hz的采样频率，所以findpeaks中的全程Distance是1868*1/100=18.68
# #所以这里的18.68成为了MinPeakDistance的范围上限
# print('-----------pks locs---------------------')
# pks
# locs
# locs = [locs(3, len(locs))]
# delta_time=np.diff(locs)# 每两次心跳时间间隔序列，去掉开头两条异常的，剩下16条，计算间隔差，所以是15条数据
# heart_rate=60/scipy.signal.mean(delta_time)# 60/心跳一次平均消耗的时间
# 图中图
# fig = plt.figure()
# left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# ax1 = fig.add_axes([left, bottom, width, height])
# ax1.plot(x, data_h, 'g')
# ax1.set_xlabel('x')
# ax1.set_ylabel('y')
# ax1.set_title('test')
plt.plot(x, data_spo2, 'r')

plt.show()
